<?php
/**
 *  喇叭道具消耗
 */

class ConsumeHrom extends APIBase {
	public $tag = "ConsumeHrom";
	//public $isLogin = false;
	
	public function before() {
		$this->initMysql();
		$this->inithttp();
		return true;
	}
			
	public function logic() {
	      
		
		if (!isset($this->param['message']) || $this->param['message']=='') {	
            $this->returnError(320, "发送消息不能为空");
			
			}

                if (isset($this->param['isplaygame']) && $this->param['isplaygame']!='') {
                        $isplaygame =  $this->param['isplaygame']; 

                        }else{
                        
                          $isplaygame = 0;
                        }

		
		//引入过滤词	
		include_once(dirname(__FILE__)."/source/badword.php");
		
		Config::$logger->info($this->tag, 'file'.dirname(__FILE__)."/source/badword.php");
		$arr = explode(',',$badword);
        $content = str_replace($arr,"***",$this->param['message']);
			
		$user = $this->data_redis->hMGet("hu:{$this->uid}", array("money","coin","bank","user","name",'is_vip'));	
		//pid 背包表pid,
		if (isset($this->param['pid']) && !empty($this->param['pid'])) {
			//$this->returnError(310, "propid must be needed");
			$id = (int)$this->param['pid'];
			$re = $this->mysql->find("select * from player_prop where id=".$id."");
			
			if($re){	
			  $this->mysql->query("delete from player_prop where id=".$id." and uid =".$this->uid."");
			  
			}else{$this->returnError(330, "pid is not exists");}
			
			
        //con_money 消耗money,propid 道具表propid  
		}else{
		
			if(!isset($this->param['propid'])||(int)$this->param['propid']<0){
			   	$this->returnError(340, "propid is not neend");
			}
			
			//$user = $this->data_redis->hMGet("hu:{$this->uid}", array("money","user"));
		     $propinfo = $this->mysql->select('goods','*',array('propid'=>(int)$this->param['propid'],'propnum'=>1)); 
			//判断是消耗元宝还是金币
			//
			
			if($propinfo[0]['buy_money'] && !$isplaygame){
				
			     $user_mc = $user['money'];
			     $contype = $propinfo[0]['buy_money'];
			     $conty_str = '乐豆';
			}elseif($propinfo[0]['buy_coin'] && !$isplaygame){
				
			     $user_mc = $user['coin'];
			     $contype = $propinfo[0]['buy_coin'];
			     $conty_str = '元宝';
			}elseif($propinfo[0]['buy_money'] && $isplaygame){

                             $user_mc = $user['bank'];
                             $contype = $propinfo[0]['buy_money'];
                             $conty_str = '银行乐豆';
                        }
			
			if(!$propinfo){
				 $this->returnError(360, "道具不存在，不能使用");
			 }	
			 
			if($user_mc<$contype){
				  $this->returnError(350, "".$conty_str.",不能使用喇叭");
			 }else{
				
				//统计用户消耗
				if($propinfo[0]['buy_money']){
				  
                                   if($isplaygame){	
			             $user['bank'] = (int)$user['bank']-(int)$propinfo[0]['buy_money'];
				     $this->data_redis->hMset("hu:{$this->uid}", array("bank"=>$user['bank']));
                                   }else{
                                     $user['money'] = (int)$user['money']-(int)$propinfo[0]['buy_money'];
                                     $this->data_redis->hMset("hu:{$this->uid}", array("money"=>$user['money']));
                                   }
				   //记录
					$m_rec['type'] = 8;
					/*$m_rec['vid'] = 0;
					$m_rec['tid']  = 0;
					$m_rec['seatid']  = 0;*/
					$m_rec['uid']  =  $this->uid;
					$m_rec['money'] = (int)$user['money'];
					$m_rec['change_money']  =  -(int)$propinfo[0]['buy_money'];
					$this->addChangeMongeyLog($m_rec);
				    //addChangeMongeLog
				  
                 }elseif($propinfo[0]['buy_coin']){
				   $user['coin'] = (int)$user['coin']-(int)$propinfo[0]['buy_coin'];
				   $this->data_redis->hMset("hu:{$this->uid}", array("coin"=>$user['coin'])); 
					 
				  }
			}
			
			//发送消息
		}
		// type 0用户发信息，1系统发
		 
         if($user['is_vip']==false || (int)$user['is_vip']==0){
			 $is_vip='0';
		 }else{
			 $is_vip='1'; 
		 }
		  
		  $value['action']='Marquee';
		  $value['receiver']='all';
		  $value['param']=json_encode(array('message'=>$content,'user'=>$user['name'],'type'=>0,'is_vip'=>$is_vip));
		  $url = http_build_query($value);
		  Config::$logger->info($this->tag, $this->uid.'----'.$user['is_vip']);
	  	  $Marqueeurl = 'http://'.Config::$ip_server_confing['ip'].':'.Config::$ip_server_confing['myport'].'/pushsrv?'.$url.'';
          Config::$logger->info($this->tag, 'Marqueeurl----'.urldecode($Marqueeurl));
		  if(!$this->http->Get($Marqueeurl)){
			 $this->returnError(390, "使用喇叭失败！");
		   }
		  $this->returnData('');
		
      //http://127.0.0.1:9090/zjhapi/api.php?action=ConsumeHrom&ver=1.5.1&uid=4853&skey=ffffffffffffffffffffffffffffffffffff&param={"message":"hello","propid":16,"pid":1}  
		
	}

	public function after() {
		$this->deinitDataRedis();
		$this->deinitMysql();
		$this->deinithttp();
	}
}

?>
